package com.bwton.msx.ccb.service.api.utils;

import android.util.Log;

import com.bwton.msx.ccb.service.api.annotations.EncryptParams;
import com.bwton.msx.ccb.service.api.annotations.TokenRequired;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.http.POST;

public class AnnotationUtils {
    public static final Class[] declaredAnnotations = {
            TokenRequired.class,
            EncryptParams.class
    };
    private static final Map<String,Method> requestMethod = new HashMap<>();
    public static void loadApiService(Class<?> apiService){
        Method[] methods = apiService.getMethods();
        for (Method method : methods) {
            POST post = method.getAnnotation(POST.class);
            if (post != null) {
                requestMethod.put(post.value(), method);
            }
        }
    }
    public static boolean tokenRequired(String path){
        Method method = requestMethod.get(path);
        if (method == null) return false;
        TokenRequired required = method.getAnnotation(TokenRequired.class);
        return required != null;
    }

    /**
     * 获取要加密的参数
     * @param path
     * @return
     */
    public static String[] encryptParams(String path){
        Method method = requestMethod.get(path);
        if (method == null) return new String[]{};
        EncryptParams encryptParams = method.getAnnotation(EncryptParams.class);
        if (encryptParams != null){
            return encryptParams.keys();
        }
        return  new String[]{};
    }
}
